﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DARE;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Text.RegularExpressions;

namespace DARE
{
    public class CDataStructuresMgr
    {
        #region fields

        private static CDataStructuresMgr _instance = null;

        #endregion

        #region properties

        public static CDataStructuresMgr Instance
        {
            get { if (_instance == null) _instance = new CDataStructuresMgr(); return _instance; }
        }

        #endregion

        #region ctor
        
        public CDataStructuresMgr()
        {
            _instance = this;
        }
        
        #endregion

        #region methods

        public DARE.Save.DataStructures.ISaveData SaveItem(object item)
        {
            if (item == null)
                return null;
            object[] types = item.GetType().GetCustomAttributes(typeof(AttrTypeDependentSerializable), false);
            if (types.Length != 1)
                return null;
            System.Reflection.ConstructorInfo ci = (types[0] as AttrTypeDependentSerializable).TypeOfDataStructure.GetConstructor(new Type[0]);
            if (ci == null)
                return null;
            DARE.Save.DataStructures.ISaveData ret = ci.Invoke(new object[0]) as DARE.Save.DataStructures.ISaveData;
            if (ret == null)
                return null;
            ret.FillDataStructure(item);
            return ret;
        }

        public object LoadItem(out object loadedItem, DARE.Save.DataStructures.ISaveData item)
        {
            loadedItem = null;
            if (item == null)
                return null;
            item.FillFromDataStructure(out loadedItem);
            return loadedItem;
        }

        #endregion
    }
}
